﻿using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Windows;

namespace Loki.UI.Wpf
{
    public class WpfWindowManager : IWindowManager
    {
        private bool? inDesignMode = null;

        public CultureInfo Culture
        {
            get { return Thread.CurrentThread.CurrentUICulture; }
        }

        public bool DesignMode
        {
            get
            {
                if (inDesignMode == null)
                {
                    var descriptor = DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement));
                    inDesignMode = (bool)descriptor.Metadata.DefaultValue;
                }

                return inDesignMode.GetValueOrDefault(false);
            }
        }

        public string GetOpenFileName(FileDialogInformations informations)
        {
            var fileDialog = new Microsoft.Win32.OpenFileDialog();
            fileDialog.Filter = informations.Filter;
            fileDialog.DefaultExt = informations.DefaultExtension;
            fileDialog.InitialDirectory = informations.InitialDirectory;
            bool result = fileDialog.ShowDialog() ?? false;

            if (result)
            {
                return fileDialog.FileName;
            }
            else
            {
                return string.Empty;
            }
        }

        public string GetSaveFileName(FileDialogInformations informations)
        {
            var fileDialog = new Microsoft.Win32.SaveFileDialog();
            fileDialog.AddExtension = true;
            fileDialog.Filter = informations.Filter;
            fileDialog.DefaultExt = informations.DefaultExtension;
            fileDialog.InitialDirectory = informations.InitialDirectory;
            bool result = fileDialog.ShowDialog() ?? false;

            if (result)
            {
                return fileDialog.FileName;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}